<?php

class Family_IndexController extends Zend_Controller_Action
{
    protected $_relation;
    protected $_familymember;
    protected $_not_allowed_array = array();
    
    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('dashboard');
        $this->_relation = new Family_Model_Relation();
        $this->_familymember = new Family_Model_Familymember();
        $this->view->headLink()->appendStylesheet('/css/colorbox.css');
        $this->view->headLink()->appendStylesheet('/css/validationEngine.css');
        $this->view->headScript()->appendFile($this->view->baseUrl() . 'js/jquery.colorbox.js', 'text/javascript' );
        $this->view->headScript()->appendFile($this->view->baseUrl() . 'js/jquery.ve.js', 'text/javascript' );
        $this->view->headScript()->appendFile($this->view->baseUrl() . 'js/jquery.ve-en.js', 'text/javascript' );
    }

    public function indexAction()
    {
        # @TODO:: find logged in user id.

        $this->view->my_rels = $this->_relation->getAllRelationArray();
        // $this->view->_family = $this->_familymember->getWholeFamily($userid);
        $user_family = $this->_familymember->getWholeFamily(1);
        $this->view->family = $user_family;

        $userObj = new Main_Model_User(); 
        // $this->view->userData = $userIbj->getUserById($id);
        $userData = $userObj->getUserById(1);
        $this->view->userData = $userData;

        $rels = $this->_relation->getAllRelations();

        $this->view->relationAddArray = $this->getAllowedRelationAdditions($rels);

        $this->view->singleRelations = $this->getSingleExistingRelations($rels);

        $this->view->userAllowedRelationAdditions = $this->getUserAllowedRelationAdditions($rels, $userData);
        $this->view->femaleRelations = $this->getFemaleRelation($rels);
    }

    /**
     *
     * @param Family_Model_Relation result set $relobj
     * @return array
     */
    protected function getFemaleRelation($relobj)
    {
        $female_relation = array('daughter', 'sister', 'wife', 'mother' );
        $female_ar = array();
        foreach($relobj as $rel)
        {
            if( in_array(strtolower($rel->RelationName), $female_relation ) )
            {
                $female_ar[$rel->id] = $rel->RelationName;
            }
        }
        return $female_ar;
    }
    /**
     *
     * @param Family_Model_Relation result set $relobj
     * @return array
     */
    protected function getAllowedRelationAdditions($relobj)
    {
        $ret_array = array();
        $i = 0;
        
        $temp_relobj = clone $relobj;
        
        foreach($relobj as $rel)
        {
            unset($temp);
            $ret_array[$rel->id]['relation_name'] = $rel->RelationName;
            if( strtolower($rel->RelationName) == 'father' )
            {
                foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'brother' ||
                        strtolower($temp->RelationName) == 'sister' ||
                        strtolower($temp->RelationName) == 'father' ||
                        strtolower($temp->RelationName) == 'mother'       )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'mother' )
            {
                foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'brother' ||
                        strtolower($temp->RelationName) == 'sister' ||
                        strtolower($temp->RelationName) == 'father' ||
                        strtolower($temp->RelationName) == 'mother'       )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'sister' )
            {
                foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'husband' ||
                        strtolower($temp->RelationName) == 'son' ||
                        strtolower($temp->RelationName) == 'daughter'    )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'brother' )
            {
                foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'wife' ||
                        strtolower($temp->RelationName) == 'son' ||
                        strtolower($temp->RelationName) == 'daughter'   )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'son' )
            {
                 foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'wife' ||
                        strtolower($temp->RelationName) == 'son' ||
                        strtolower($temp->RelationName) == 'daughter'   )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'daughter' )
            {
                 foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'husband' ||
                        strtolower($temp->RelationName) == 'son' ||
                        strtolower($temp->RelationName) == 'daughter'   )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'wife' )
            {
                foreach($temp_relobj as $temp)
                {
                    if( strtolower($temp->RelationName) == 'brother' ||
                        strtolower($temp->RelationName) == 'sister' ||
                        strtolower($temp->RelationName) == 'father' ||
                        strtolower($temp->RelationName) == 'mother'       )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
            elseif( strtolower($rel->RelationName) == 'husband' )
            {
                foreach($temp_relobj as $temp)
                {
                    if( strtolower($rel->RelationName) == 'brother' ||
                        strtolower($rel->RelationName) == 'sister' ||
                        strtolower($rel->RelationName) == 'father' ||
                        strtolower($rel->RelationName) == 'mother'       )
                    {
                        $ret_array[$rel->id]['can_add_relations'][] = array( 'id'=> $temp->id , 'relation_name' => $temp->RelationName   );
                    }
                }
            }
           
            $i++;
        }
       
        return $ret_array;
    }

    /**
     *
     * @param Family_Model_Relation result set $relobj
     * @return array
     */
    protected function getSingleExistingRelations($relobj)
    {
        $ret_ar = array();
        foreach ($relobj as $rel)
        {
            if(strtolower($rel->RelationName) == 'father' ||
               strtolower($rel->RelationName) == 'mother' ||
               strtolower($rel->RelationName) == 'husband' ||
               strtolower($rel->RelationName) == 'wife' )
            {
                $ret_ar[$rel->id] =  $rel->RelationName  ;
            }
        }

        return $ret_ar  ;
    }

    protected function getUserAllowedRelationAdditions($relObj, $userData)
    {
        $key_single_relation = array_keys($this->getSingleExistingRelations($relObj));
        $familydata = $this->_familymember->getUserFamilyTableData(1);

        $allowed_relation_addition = array('father', 'mother', 'sister', 'brother', 'son', 'daughter'  );

        if($userData->Gender)
        {
            // male
            $allowed_relation_addition [] = 'wife';
        }
        else
        {
            // female
            $allowed_relation_addition [] = 'husband';
        }
        
        $final_allowed_relation_addition = array();
        foreach($relObj as $ro)
        {
            if(in_array(strtolower($ro->RelationName), $allowed_relation_addition ))
            {
                $final_allowed_relation_addition[$ro->id] = $ro->RelationName;
            }
        }

        $direct_added = array();
        foreach($familydata as $fam)
        {
            foreach($relObj as $ro)
            {
                if($fam->relation_id == $ro->id)
                {
                    $direct_added[] =   $ro->RelationName  ;
                }
            }
        }

        $direct_added_relation = array();
        
        foreach($final_allowed_relation_addition as $relid =>  $temp)
        {
        
            if(in_array( $temp,  $direct_added ))
            {
               continue;
            }
             $direct_added_relation[$relid] = $temp;
        }
        return $direct_added_relation;
        
    }

    public function saverelationAction()
    {
        // @TODO:: find user_id form loggedin user.
        $requst_params = $this->getRequest()->getParams();
        $gender = 'F';
        if( $requst_params['gender'] == 'Male' )
        {
            $gender = 'M';
        }

        $save_data = array(
                            'FirstName' =>  $requst_params['firstname'],
                            'LastName'  =>  $requst_params['lastname'],
                            'MiddleName'=>  $requst_params['middlename'],
                            'Gender'    =>  $gender
                            );
        //print_r($save_data);
       
        // save family member info;
        $fmiobj = new Family_Model_Familymemberinfo();
        $ins_id = $fmiobj->save($save_data);
        // echo $ins_id;

        $member = new Family_Model_Familymember();
        // find lineage for the given info
        
        $parent = $member->getMemberById($this->getRequest()->getParam('parent_id'));
        
        foreach ($parent as $p)
        {
            $lineage = $p->family_lineage;
            break;
        }

        //echo '<br> $lineage:: ', $lineage;

        $new_lineage = $lineage. '/' . $this->getRequest()->getParam('relation_id')  ;
        
        // save into family table
        
        $member_data = array(
                                'userid'        => 1,       // need to change
                                'relation_id'   => $this->getRequest()->getParam('relation_id'),
                                'parent_id'     => $this->getRequest()->getParam('parent_id'),
                                'isRoot'        => 0,
                                'fmid'          => $ins_id,
                                'family_lineage'=> $new_lineage

                            );
        //print_r($member_data);
        $member = new Family_Model_Familymember();
        $member->save($member_data);
        echo '1';
        exit;
    }
}
?>